package zhr.chapter9;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class WindowCalender extends JFrame implements ItemListener, TreeSelectionListener {
    JTable table;
    JTree tree = null; //树组件
    DefaultMutableTreeNode root;
    Object name[]={"星期日","星期一","星期二","星期三", "星期四","星期五","星期六"};
    JComboBox yearList;
    int year, month;
    CalendarBean calendar;
    String rili[][];
    String item[]={"2024","2025","2026","2027","2028","2029","2030" };
    JScrollPane scrollTable, scrollTree;
    JSplitPane split;
    WindowCalender() {
        init();
        setSize(580,200);
        setVisible( true);
        setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE);
    }
    void init() {
        calendar = new CalendarBean();
        yearList = new JComboBox();
        for( int k= 0;k< item. length;k++) {
            yearList. addItem( item[k]);
        }
        yearList. addItemListener( this);
        root= new DefaultMutableTreeNode( item[0]); //创建根结点
        year= Integer. parseInt( item[0]);
        month=1;
        DefaultMutableTreeNode [] 月 = new DefaultMutableTreeNode[13]; //子结点数组
        for( int i=1;i<= 12;i++) {
            月[i]= new DefaultMutableTreeNode("" + i);
            root. add(月[i]);
        }
        tree = new JTree( root);
        add( new JScrollPane( tree), BorderLayout. WEST);
        tree. addTreeSelectionListener( this); //监视树结点事件
        calendar.setYear( year);
        calendar.setMonth( month);
        rili= calendar. getCalendar();
        table= new JTable( rili, name);
        scrollTree= new JScrollPane( tree);
        scrollTable= new JScrollPane( table);
        split= new JSplitPane(JSplitPane. HORIZONTAL_SPLIT, true, scrollTree, scrollTable);
        split. setDividerLocation(0.5);
        add(yearList, BorderLayout. NORTH);
        add( split, BorderLayout. CENTER);
    }
    public void itemStateChanged(ItemEvent e) { //处理下拉列表上的 ItemEvent 事件
        String 年 = yearList. getSelectedItem(). toString(). trim();
        year = Integer. parseInt(年);
        calendar. setYear( year);
        root = new DefaultMutableTreeNode(年); //重新创建根结点
        DefaultMutableTreeNode 月[]= new DefaultMutableTreeNode[13]; //子结点数组
        for( int i=1;i<= 12;i++) {
            月[i]= new DefaultMutableTreeNode("" + i);
            root. add(月[i]);
        }
        split. remove(scrollTree);
        tree = new JTree( root);
        tree. addTreeSelectionListener( this);
        scrollTree= new JScrollPane( tree);
        split. add(scrollTree,JSplitPane. LEFT);
    }
    public void valueChanged(TreeSelectionEvent e) { //处理 TreeSelectionEvent 事件
        DefaultMutableTreeNode monthNode =
        (DefaultMutableTreeNode) tree. getLastSelectedPathComponent();
        if(monthNode. isLeaf()) {
            month= Integer. parseInt(monthNode. toString(). trim());
            calendar.setMonth( month); rili= calendar.getCalendar();
            split. remove(scrollTable);
            table = new JTable( rili, name);
            scrollTable= new JScrollPane( table);
            split. add(scrollTable,JSplitPane. RIGHT);
        }
    }
}

